package com.flowershop.dao;

import com.flowershop.model.CartItem;
import com.flowershop.util.DBUtil;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class CartDao {
    
    public List<CartItem> getCartItems(Integer userId) {
        List<CartItem> items = new ArrayList<>();
        String sql = "SELECT c.*, f.name as flower_name, f.price, f.image_url " +
                    "FROM shopping_cart c " +
                    "JOIN flowers f ON c.flower_id = f.id " +
                    "WHERE c.user_id = ?";
        
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, userId);
            ResultSet rs = pstmt.executeQuery();
            
            while (rs.next()) {
                CartItem item = new CartItem();
                item.setId(rs.getInt("id"));
                item.setUserId(rs.getInt("user_id"));
                item.setFlowerId(rs.getInt("flower_id"));
                item.setQuantity(rs.getInt("quantity"));
                item.setCreateTime(rs.getTimestamp("create_time"));
                item.setUpdateTime(rs.getTimestamp("update_time"));
                item.setFlowerName(rs.getString("flower_name"));
                item.setPrice(rs.getBigDecimal("price"));
                item.setImageUrl(rs.getString("image_url"));
                items.add(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        return items;
    }
    
    public boolean addToCart(CartItem item) {
        // 先检查是否已存在
        String checkSql = "SELECT id, quantity FROM shopping_cart WHERE user_id = ? AND flower_id = ?";
        String insertSql = "INSERT INTO shopping_cart (user_id, flower_id, quantity) VALUES (?, ?, ?)";
        String updateSql = "UPDATE shopping_cart SET quantity = quantity + ? WHERE id = ?";
        
        try (Connection conn = DBUtil.getConnection()) {
            // 检查是否已存在
            try (PreparedStatement checkStmt = conn.prepareStatement(checkSql)) {
                checkStmt.setInt(1, item.getUserId());
                checkStmt.setInt(2, item.getFlowerId());
                ResultSet rs = checkStmt.executeQuery();
                
                if (rs.next()) {
                    // 已存在，更新数量
                    try (PreparedStatement updateStmt = conn.prepareStatement(updateSql)) {
                        updateStmt.setInt(1, item.getQuantity());
                        updateStmt.setInt(2, rs.getInt("id"));
                        return updateStmt.executeUpdate() > 0;
                    }
                } else {
                    // 不存在，新增
                    try (PreparedStatement insertStmt = conn.prepareStatement(insertSql)) {
                        insertStmt.setInt(1, item.getUserId());
                        insertStmt.setInt(2, item.getFlowerId());
                        insertStmt.setInt(3, item.getQuantity());
                        return insertStmt.executeUpdate() > 0;
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean updateQuantity(Integer id, Integer quantity) {
        String sql = "UPDATE shopping_cart SET quantity = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, quantity);
            pstmt.setInt(2, id);
            return pstmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    
    public boolean removeFromCart(Integer id) {
        String sql = "DELETE FROM shopping_cart WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            
            pstmt.setInt(1, id);
            return pstmt.executeUpdate() > 0;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
} 